import {UserActions} from './user';

const initialState = {};

export const SupplierTopActions = {
  SUPPLIER_TOP_UPDATE: 'supplier_top_update',
  SUPPLIER_TOP_REMOVE: 'supplier_top_remove',
  SUPPLIER_TOP_RESET: 'supplier_top_reset',
};

export function updateSupplierTop(payload) {
  return {
    type: SupplierTopActions.SUPPLIER_TOP_UPDATE,
    payload,
  };
}

export function removeSupplierTop(payload) {
  return {
    type: SupplierTopActions.SUPPLIER_TOP_REMOVE,
    payload,
  };
}

export function resetSupplierTop() {
  return {
    type: SupplierTopActions.SUPPLIER_TOP_RESET,
  };
}

export default function supplierTop(state = initialState, action) {
  switch (action.type) {
    case SupplierTopActions.SUPPLIER_TOP_UPDATE:
      action.payload.forEach(item => {
        state[item.id] = item;
      });
      return {...state};
    case SupplierTopActions.SUPPLIER_TOP_REMOVE:
      delete state[action.payload.id];
      return {...state};
    case SupplierTopActions.SUPPLIER_TOP_RESET:
      return {};
    case UserActions.USER_SIGN_OUT:
      return {};
    default:
      return state;
  }
}
